﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace IAlarm
{
    /// <summary>
    /// 报警操作的类型
    /// </summary>
    public enum GroupOperate : int
    {
        /// <summary>
        /// 无操作
        /// </summary>
        None = 0,
        /// <summary>
        /// 确定为火警
        /// </summary>
        Yes = 1,
        /// <summary>
        /// 确定不为火警
        /// </summary>
        No = 2,
        /// <summary>
        /// 不确定为火警
        /// </summary>
        Unknown = 3
    }

    /// <summary>
    /// 报警组信息
    /// </summary>
    public sealed class GroupInfo
    {
        #region 私有变量
        //报警ID列表
        List<long> alarmIDs = new List<long>();

        //报警信息列表
        Dictionary<long, AlarmInfo> alarmInfos = new Dictionary<long, AlarmInfo>();
        #endregion

        #region 公有属性
        /// <summary>
        /// 报警组ID
        /// </summary>
        public long GroupID { get { return GroupTime.Ticks; } }

        /// <summary>
        /// 报警组时间
        /// </summary>
        public readonly DateTime GroupTime;

        /// <summary>
        /// 区域名
        /// </summary>
        public readonly string Area;

        /// <summary>
        /// 设备名
        /// </summary>
        public readonly string Device;

        /// <summary>
        /// 水平角度
        /// </summary>
        public readonly short HorizonAngle;

        /// <summary>
        /// 垂直角度
        /// </summary>
        public readonly short VerticalAngle;

        /// <summary>
        /// 识别总帧数
        /// </summary>
        public readonly int AnalyzeCount;

        /// <summary>
        /// 报警事件操作类型
        /// </summary>
        public GroupOperate GroupOperate { get; set; }

        /// <summary>
        /// 返回指定序号的报警信息
        /// </summary>
        /// <param name="index">序号</param>
        /// <returns>报警信息</returns>
        public AlarmInfo this[int index]
        {
            get
            {
                if (index >= alarmIDs.Count || !alarmInfos.ContainsKey(alarmIDs[index]))
                    return null;
                else
                    return alarmInfos[alarmIDs[index]];
            }
        }

        /// <summary>
        /// 报警数量
        /// </summary>
        public int AlarmCount { get { return this.alarmIDs.Count; } }

        #endregion

        #region 公有方法
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="groupID">报警帧组ID</param>
        /// <param name="area">区域名</param>
        /// <param name="device">设备名</param>
        /// <param name="horizon">水平角度</param>
        /// <param name="vertical">垂直角度</param>
        /// <param name="count">识别总帧数</param>
        public GroupInfo(DateTime groupTime, string area, string device, short horizon, short vertical, int count)
        {
            this.GroupTime = groupTime;
            this.Area = area;
            this.Device = device;
            this.HorizonAngle = horizon;
            this.VerticalAngle = vertical;
            this.AnalyzeCount = count;
        }

        public GroupInfo(long groupID, string area, string device, short horizon, short vertical, int count) : this(new DateTime(groupID), area, device, horizon, vertical, count) { }

        /// <summary>
        /// 向报警帧组ID中加入一个报警信息
        /// </summary>
        /// <param name="id">报警帧ID</param>
        /// <param name="eventInfo">报警信息</param>
        public void Add(AlarmInfo alarm)
        {
            long id = alarm.AlarmTime.Ticks;

            if (!this.alarmInfos.ContainsKey(id))
            {
                this.alarmInfos.Add(id, alarm);
                this.alarmIDs.Add(id);
            }
        }

        /// <summary>
        /// 将报警帧组ID转换成日期时间格式的字符串
        /// </summary>
        /// <returns>日期时间格式的字符串</returns>
        public override string ToString()
        {
            return new DateTime(this.GroupID).ToString();
        }
        #endregion

    }

    /// <summary>
    /// 报警信息
    /// </summary>
    public sealed class AlarmInfo
    {
        /// <summary>
        /// 报警组ID
        /// </summary>
        public long GroupID { get { return GroupTime.Ticks; } }

        /// <summary>
        /// 报警组时间
        /// </summary>
        public readonly DateTime GroupTime;

        /// <summary>
        /// 报警ID
        /// </summary>
        public long AlarmID { get { return AlarmTime.Ticks; } }

        /// <summary>
        /// 报警帧组中序号
        /// </summary>
        public readonly int FrameIndex;
 
        /// <summary>
        /// 报警时间
        /// </summary>
        public readonly DateTime AlarmTime;

        /// <summary>
        /// 报警区域组
        /// </summary>
        public readonly Rectangle[] AlarmRegion;

        /// <summary>
        /// 报警区域类型组
        /// </summary>
        public readonly int[] AlarmRegionType;

        /// <summary>
        /// 视频报警事件参数
        /// </summary>
        /// <param name="groupID">报警帧组ID</param>
        /// <param name="frameIndex">报警帧组中序号</param>
        /// <param name="alarmTime">报警时间</param>
        /// <param name="alarmRegion">报警区域组</param>
        /// <param name="alarmRegionType">报警区域类型组</param>
        public AlarmInfo(DateTime groupTime, int frameIndex, DateTime alarmTime, Rectangle[] alarmRegion, int[] alarmRegionType)
        {
            this.GroupTime = groupTime;
            this.FrameIndex = frameIndex;
            this.AlarmTime = alarmTime;
            this.AlarmRegion = alarmRegion;
            this.AlarmRegionType = alarmRegionType;
        }

        public AlarmInfo(long groupID, int frameIndex, long alarmID, Rectangle[] alarmRegion, int[] alarmRegionType) : this(new DateTime(groupID), frameIndex, new DateTime(alarmID), alarmRegion, alarmRegionType) { }
    }
}
